//
//  XIUGaiNiChengViewController.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/4/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "XIUGaiNiChengViewController.h"

@interface XIUGaiNiChengViewController ()

@property (nonatomic, strong)UITextField *nameTextField;

@end

@implementation XIUGaiNiChengViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"修改昵称";
    
    [self setUI];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(onClickedOKbtn)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_nameTextField resignFirstResponder];
}


-(void)setUI{
    
    UILabel *nameLable = [[UILabel alloc]initWithFrame:CGRectMake(20 * KscreenWidth,30 * KscreenHeight + NAVIGATION_BAR_HEIGHT,65* KscreenWidth, 20 * KscreenHeight)];
    nameLable.text = @"昵称:";
    nameLable.font = [UIFont systemFontOfSize:18 * KscreenWidth];
    nameLable.textColor = hui7Color;
    [self.view addSubview:nameLable];
    
    _nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(85 * KscreenWidth, 30 * KscreenHeight + NAVIGATION_BAR_HEIGHT, 200 * KscreenWidth, 20 * KscreenHeight)];
    _nameTextField.textAlignment = NSTextAlignmentLeft;
    _nameTextField.text = [ZYUserManager userInfo].nick_name;
    _nameTextField.font = [UIFont systemFontOfSize:18 * KscreenWidth];
    _nameTextField.textColor = hui7Color;
    [self.view addSubview:_nameTextField];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(20 * KscreenWidth, 60 * KscreenHeight + NAVIGATION_BAR_HEIGHT, ScreenWidth - 40 * KscreenWidth, 1 * KscreenHeight)];
    line.backgroundColor = ZhuTiColor;
    [self.view addSubview:line];
    
}
- (void)onClickedOKbtn {
    
    if (_nameTextField.text.length > 6) {
        [MBProgressHUD showTextOnly:self.view message:@"昵称不能超过6个字" delay:2.0];
        return;
    }
    
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"nick_name":_nameTextField.text,
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Ucenter&a=changeNickname",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"status"] intValue] == 200) {
            [MBProgressHUD showTextOnly:self.view message:@"修改成功" delay:2.0];
            
            UserinfoModel *model = [ZYUserManager userInfo];
            model.nick_name = _nameTextField.text;

            [ZYUserManager saveUserInfo:model];
            
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            
            [MBProgressHUD showTextOnly:self.view message:responseObject[@"msg"] delay:2.0];
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
